﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.ComponentModel;
using System.Reflection;

namespace Proggy.Web.Cms.Core.Configuration
{
    /// <summary>
    /// Part element model
    /// </summary>
    public class DragAndDropPartElement : ConfigurationElement
    {
        /// <summary>
        /// Alias / Key
        /// </summary>
        [ConfigurationProperty("name", DefaultValue = "Unknown", IsKey = true, IsRequired = true)]
        public string Name
        {
            get { return (string)base["name"]; }
            set { base["name"] = value; }
        }

        [ConfigurationProperty("hasAutoRenderModel", IsRequired = true)]
        public bool HasAutoRenderModel
        {
            get { return (bool)base["hasAutoRenderModel"]; }
            set { base["hasAutoRenderModel"] = value; }
        }

        [ConfigurationProperty("friendlyName", IsRequired = true)]
        public string FriendlyName
        {
            get { return (string)base["friendlyName"]; }
            set { base["friendlyName"] = value; }
        }

        [ConfigurationProperty("description", IsRequired = true)]
        public string Description
        {
            get { return (string)base["description"]; }
            set { base["description"] = value; }
        }

        /// <summary>
        /// If partial, full path to partial
        /// </summary>
        [ConfigurationProperty("contentPartial", IsRequired = false)]
        public string ContentPartial
        {
            get { return (string)base["contentPartial"]; }
            set { base["contentPartial"] = value; }
        }

        /// <summary>
        /// If has auto render model, full type name for render model
        /// </summary>
        [TypeConverter(typeof(TypeNameConverter))]
        [ConfigurationProperty("propertyType", IsRequired = false)]
        public Type PropertyType
        {
            get
            {
                return this["propertyType"] as Type;
            }
            set
            {
                // set
                base["propertyType"] = value;
            }
        }

        /// <summary>
        /// If has auto render model, full type name for render model
        /// </summary>
        [TypeConverter(typeof(TypeNameConverter))]
        [ConfigurationProperty("staticConfigObjectType", IsRequired = false)]
        public Type StaticConfigObjectType
        {
            get
            {
                return this["staticConfigObjectType"] as Type;
            }
            set
            {
                // set
                base["staticConfigObjectType"] = value;
            }
        }

        public object StaticConfigObject
        {
            get
            {
                object output = null;

                if (this.StaticConfigObjectType != null)
                {
                    // get props
                    var props = this.StaticConfigObjectType.GetProperties();

                    // get prop
                    var theProp = props.Single(x => x.Name.ToLower() == this.Name.ToLower());

                    // return
                    return theProp.GetValue(this.StaticConfigObjectType, null);
                }

                //
                return output;
            }
        }
    }
}
